package com.ea.holder;

import com.ea.common.enums.OperationResultCode;
import com.ea.common.enums.YesOrNoEnum;
import com.ea.common.exception.ServiceException;
import com.ea.domain.entity.AdminInfo;
import com.ea.domain.entity.UserInfo;

import java.util.Objects;

/**
 * @author NAME-L
 * @Description TODO
 * @className AdminHolderInfo
 * @date 2023-01-31 19:18:34
 */
public class AdminHolderInfo {
    private static final ThreadLocal<AdminInfo> adminThreadLocal = new ThreadLocal<>();


    public static AdminInfo getAdminInfo() {
        return adminThreadLocal.get();
    }

    public static void setAdminInfo(AdminInfo adminInfo) {
        adminThreadLocal.set(adminInfo);
    }

    public static Long getAdminId() {
        AdminInfo adminInfo = getAdminInfo();
        return Objects.isNull(adminInfo) ? 0L : adminInfo.getAdminId();
    }

    public static String getAdminName() {
        AdminInfo adminInfo = getAdminInfo();
        return Objects.isNull(adminInfo) ? null : adminInfo.getAdminName();
    }

    public static void getAuth() {
        AdminInfo adminInfo = getAdminInfo();
        if (YesOrNoEnum.NO.getCode().equals(adminInfo.getIsSuper())) {
            throw new ServiceException(OperationResultCode.AUTH_FORBID);
        }
    }
    public static void clearAdminCache() {
        adminThreadLocal.remove();
    }

}
